<?php
// misc
function dbsize() {
  return round(filesize("database.sqlite")/1024);
}
function getlink($ns,$name) {
  return "<a href='?$ns=$name'>$name</a>";
}
// theme helpers
function textilize($text) {
  include_once("Textile.php");
  $textile = new Textile;
  return $textile->process($text);
}
/* this baby is important. improvements needed:
* check if pages exist, make 'em look different
* absolute links (why...?)
** mod_rewrite on/off
*/
function parselinks($input) {
  $final = "";
  $split = split("}",$input);
  for($i=0; $i<count($split)-1; $i++) {
    $segment = $split[$i];
    $split2 = split("{",$segment);
    $final .= $split2[0];
    $linktarget = $split2[1];
    // links should be absolute...
    $final .= getlink("show",$linktarget);
  }
  $final .= $split[count($split)-1];
  return $final;
}
function listTags($tagstring) {
  $tags = array();
  foreach(split(" ",$tagstring) as $tag) {
    array_push($tags,"<a href='?tag=$tag'>$tag</a>");
  }
  echo join($tags,", ");
}
function listLinkedToBy($pagelist) {
  if(count($pagelist) > 0) {
    $links = array();
    foreach($pagelist as $page) {
      array_push($links,"<a href='?show=$page'>$page</a>");
    }
    echo join($links,", ");
  } else {
    echo "none";
  }
}
function listChildren($db,$pagename) {
  $children = sqlite_fetch_all(sqlite_query($db,"SELECT title FROM pages WHERE parent LIKE '$pagename'"));
  if(count($children) > 0) {
    $links = array();
    foreach($children as $child) {
      $childname = $child['title'];
      array_push($links,"<a href='?show=$childname'>$childname</a>");
    }
    echo join($links,", ");
  } else {
    echo "none";
  }
}
function showParent($parentname) {
  if($parentname == "none") {
    echo "none";
  } else {
    echo "<a href='?show=$parentname'>$parentname</a>";
  }
}
function showPageOutline($db,$parent,$currentpage) {
  echo "<ul>\n";
  $pages = sqlite_fetch_all(sqlite_query($db,"SELECT * FROM pages"));
  foreach($pages as $page) {
    $title = $page['title'];
    if($page['parent'] == $parent) {
      if($title == $currentpage) {
        echo "<li><strong><a href='?show=$title'>$title</a></strong></li>";
      } else {
        echo "<li><a href='?show=$title'>$title</a></li>\n";
      }
      showPageOutline($db,$title,$currentpage);
    }
  }
  echo "</ul>\n";
}
?>